//app.js
//接口域名
//const host = 'http://localhost:5260/api/';
const host = 'http://api.itobest.com/api/';
//封装请求方法
const request = function request(option) {
  wx.request({
    url: host + option.url,
    data: option.data,
    method: option.method,
    header: {
      'content-type': 'application/json', // 默认值
      'Authorization': 'token ' + wx.getStorageSync('token'),
      "Access-TimeSpan": (new Date()).valueOf(),
      'userid': wx.getStorageSync('userid')
    },
    success: function (res) {
      if (typeof option.success == "function") {
        option.success(res);
      }
    },
    fail: function () {
      if (typeof option.fail == "function") {
        option.fail();
      }
    }
  });
};
//封装文件上传方法
const uploadFile = function uploadFile(option) {
  wx.uploadFile({
    url: host + option.url,
    filePath: option.filePath,
    name: 'file',
    header: {
      'Authorization': 'token ' + wx.getStorageSync('token'),
      "Access-TimeSpan": (new Date()).valueOf(),
      'userid': wx.getStorageSync('userid')
    },
    success: function (res) {
      if (typeof option.success == "function") {
        option.success(res);
      }
    },
    fail: function () {
      if (typeof option.fail == "function") {
        option.fail();
      }
    }
  });
};
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登陆
    var that = this;
    // wx.login({
    //   success: res => {
    //     // 发送 res.code 到后台换取 openId, sessionKey, unionId
    //     // console.log(res)
    //     wx.request({
    //       url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wx9cc44b705f342057&secret=71d424a2df74e99f473b51a46430fecd&js_code=' + res.code + '&grant_type=authorization_code',
    //       method: 'POST',
    //       success: function (r) {
    //         wx.request({
    //           url: host + 'auth/wxlogin',
    //           data: { 'wxopenid': r.data.openid },
    //           method: 'POST',
    //           success: function (res) {
    //             console.log(res)
    //             wx.setStorageSync('token', res.data.token);
    //             wx.setStorageSync('userid', res.data.userinfo.UserID);
    //           }
    //         })
    //       }
    //     })
    //   }
    // })

    //获取用户信息
    wx.getUserInfo({
      success: res => {
        // 可以将 res 发送给后台解码出 unionId
        this.globalData.userInfo = res.userInfo
        // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        if (this.userInfoReadyCallback) {
          this.userInfoReadyCallback(res)
        }
      },
      fail: function () {
        wx.showModal({
          title: '提示',
          content: '请重新授权后才能正常使用小程序',
          showCancel:false,
          success: function (res) {
            if (res.confirm) {
              // 重新发起授权
              wx.reLaunch({
                url: '/pages/logon/error/error',
              })
            }
          }
        })
      }
    })
  },
  globalData: {
    userInfo: null
  },
  _request: function (option) {
    request(option);
  },
  _uploadFile: function (option) {
    uploadFile(option);
  },
  _host: host,
})